;=======================================
;Galaxo Blast
;(Title screen code)
;
;(C)2026 The New Dimension
;Written by Richard Bayliss
;
;For Fun and Games Programming in
;Assembly - Part 3
;
;Memory locations used:
;----------------------
;
;     --- GAME BITS ---
;SPRITES      $2000-$2CFF
;GAME SCREEN  $3000-$33E7
;GAME COLOUR  $3400-$37E7
;CHARSET      $3800-$3EFF
;GAME CODE    $3F00-$4FFF
;SFX PLAYER   $5000-$53FF
; --- TITLE SCREEN BITS ---
;MUSIC        $0D00-$1FFF
;TITLE CODE   $5400-$57ff
;LOGO COLRAM  $5800-$5be7
;LOGO VIDRAM  $5c00-$5de7
;LOGO BITMAP  $6000-$7000
;SCREEN TEXT  $7000-$7400
;SCREEN COL   $7400-$77E7
;SCROLLTEXT   $7800-$7FFF
;-------------------------------------
;Varibles/labels

;Define game jump start address
gamecode = $3f00

;Sound and music parameters
musicinit = $0d00
musicplay = $0d03

;Scorepanel update routine location
panelcode = $4a56

;Define colour and screen RAM
scrn2    = $0400
colr2    = $d800

;Define VIDCOM paint bitmap colour
;data location
bmpcol   = $5800

;Define title screen video RAM data
titlescrn = $7000

;Define title screen colour RAM data
titlecol = $7400 ;Title colour data

;Deefine scroll text location
scrolltext = $7800 ;Scroll text data

;======================================
; Define start address for title code
         *= $5400
;======================================



;Kill off all existing IRQ raster
;interrupts and blackout the screen

syscheck
         ;Check the raster line
         lda $d012
         cmp $d012
         beq *-3
         bmi syscheck
         cmp #$20
         bcc ntscfound2

;PAL system is found set pointer system2
;as 1.

         lda #1
         sta system2
         jmp main

;NTSC detected, set pointer system2 as 0

ntscfound2 lda #$00
         sta system2

;Main title screen code

main
         sei
         lda #$36
         sta $01
         ldx #$fb
         txs

; Disables RUN/STOP and RESTORE, if
; using TURBO MACRO PRO for testing
; on your C64. Comment the command and
; uncomment this below to allow access
; to your assembler.

         lda #251
         sta $0328

;Press RESTORE to jump back to Turbo
;Macro Pro V1.2+ assembler.

;assembler = $0140 ;(if using REU)
;assembler = $8000 ;(not using REU)
;assembler = $9000 ;Turbo Assembler

;                 ldx #<assembler
;        ldy #>assembler
;                 stx $0328
;                 sty $0329


         ldx #$31
         ldy #$ea
         lda #$81
         stx $0314
         sty $0315
         sta $dc0d
         sta $dd0d
         lda #$00
         sta $d019
         sta $d01a
         sta $d020
         sta $d021
         sta $d011
         sta $d015
         ldx #$00
zpos     lda #$00
         sta $d000,x
         inx
         cpx #$10
         bne zpos

;Clear SID

         ldx #$00
clearsid2
         lda #$00
         sta $d400,x
         inx
         cpx #$18
         bne clearsid2
;Clear whole screen

         ldx #$00
tsclr
         lda #$20
         sta scrn2,x
         sta scrn2+$0100,x
         sta scrn2+$0200,x
         sta scrn2+$02e8,x
         sta colr2,x
         sta colr2+$0100,x
         sta colr2+$0200,x
         sta colr2+$02e8,x
         inx
         bne tsclr

  ;Reset colour wash pointer

         lda #0
         sta washptr
         sta washdelay

  ;Setup VIC2 colour settings

         lda #0
         sta $d020
         sta $d021
         lda #$0b
         sta $d022
         lda #$01
         sta $d023

;Draw potion of the title screen
;graphics to the colour and screen RAM

         ldx #$00
drawtitle
         lda titlescrn+(2*240),x
         sta scrn2+(2*240),x
         lda titlescrn+(3*240),x
         sta scrn2+(3*240),x
         lda titlescrn+(3*240)+40,x
         sta scrn2+(3*240)+40,x
         lda titlecol+(2*240),x
         sta colr2+(2*240),x
         lda titlecol+(3*240),x
         sta colr2+(3*240),x
         lda titlecol+(3*240)+40,x
         sta colr2+(3*240)+40,x
         inx
         cpx #240
         bne drawtitle

;Now setup the bitmap colour data

         ldx #$00
putcols
         lda bmpcol,x
         sta colr2,x
         lda bmpcol+240,x
         sta colr2+240,x
         inx
         cpx #240
         bne putcols

; Initialise fire button press

         lda #0
         sta firebutton2

; Update the score panel once

;NOTE: If you are testing this title
;      screen in TURBO MACRO PRO, by
;      running it. Comment out the
;      jump subroutine command below

         jsr panelcode

; Initialise the scroll text

         lda #<scrolltext
         sta messread+1
         lda #>scrolltext
         sta messread+2

         ldx #$00
clrscrl
         lda #$20
         sta scrn2+(21*40),x
         inx
         cpx #40
         bne clrscrl

         lda #$07
         sta xpos

; Delay the title screen before
; executing it. This is to prevent
; sensitivity after pressing the fire
; button on game over/game complete.

         ldx #$00
del001   ldy #$00
del002   iny
         bne del002
         inx
         bne del001

;--------------------------------------

;Setup title screen IRQs
;--------------------------------------

         ldx #<tirq1
         ldy #>tirq1

         stx $0314
         sty $0315
         lda #$7f
         sta $dc0d
         sta $dd0d
         lda #$2e
         sta $d012
         lda #$1b
         sta $d011
         lda #$01
         sta $d01a
         lda #0
         jsr musicinit
         cli
         jmp titleloop

;IRQ 1 - play music and setup raster
;sync timer

tirq1
         inc $d019
         lda $dc0d
         sta $dd0d
         lda #$2e
         sta $d012
         lda #1
         sta rt2
         jsr musicplayer
         ldx #<tirq2
         ldy #>tirq2
         stx $0314
         sty $0315
         jmp $ea7e

musicplayer
         lda system2
         bne palmus
         inc ntsctimer2
         lda ntsctimer2
         cmp #6
         beq loopntsc2
palmus
         jsr musicplay
         rts
loopntsc2
         lda #0
         sta ntsctimer2
         rts

;IRQ 2 - display multicoloured bitmap
;logo

tirq2
         inc $d019
         lda #$91
         sta $d012
         lda #$3b
         sta $d011
         lda #$18
         sta $d016
         lda #$78
         sta $d018
         lda #$02
         sta $dd00

         ldx #<tirq3
         ldy #>tirq3
         stx $0314
         sty $0315
         jmp $ea7e

;IRQ 3 - display charset in hi-res mode

tirq3
         inc $d019
         lda #$da
         sta $d012
         ldx #$0b
         dex
         bne *-1
         lda #$1b
         sta $d011
         lda #$00
         sta $d016
         lda #$1e
         sta $d018
         lda #$03
         sta $dd00
         ldx #<tirq4
         ldy #>tirq4
         stx $0314
         sty $0315
         jmp $ea7e

;IRQ 4 - setup the smooth 1x1 scroll
;text message

tirq4
         inc $d019
         lda #$e2
         sta $d012

         lda xpos
         sta $d016
         ldx #<tirq5
         ldy #>tirq5
         stx $0314
         sty $0315
         jmp $ea7e

;IRQ 5 - display multicoloured still
;characters again

tirq5
         inc $d019
         lda #$f9
         sta $d012
         lda #$18
         sta $d016

         ldx #<tirq1
         ldy #>tirq1
         stx $0314
         sty $0315
         jmp $ea7e
;-------------------------------------
;Main title screen loop
;-------------------------------------
titleloop

;Manually synchronise the synctimer
;(rt) with the irq raster interrupts

         lda #0
         sta rt2
         cmp rt2
         beq *-3

; Call smooth scroll text routine

         jsr scroller

; Call colour wash routine and text
; flashing routine

         jsr colwash

; Wait for fire button to be pressed
; in order to start the game.

         lda $dc00
         lsr a
         lsr a
         lsr a
         lsr a
         lsr a
         bit firebutton2
         ror firebutton2
         bmi titleloop
         bvc titleloop
         jmp gamecode

;--------------------------------------

;Scroll text routine
;--------------------------------------

scroller
         lda xpos
         sec
         sbc #2;Scroll speed
         and #7
         sta xpos
         bcs exitscr
         ldx #$00
scrloop
         lda scrn2+(21*40)+1,x
         sta scrn2+(21*40),x
         inx
         cpx #$28
         bne scrloop
messread
         lda scrolltext
         cmp #$00
         bne stortxt
         lda #<scrolltext
         sta messread+1
         lda #>scrolltext
         sta messread+2
         jmp messread
stortxt
         sta scrn2+(21*40)+39
         inc messread+1
         bne exitscr
         inc messread+2
exitscr
         rts

;-------------------------------------
;Colour washing routine
;-------------------------------------

colwash
         jsr colwash2
         lda washdelay
         cmp #1
         beq dowash
         inc washdelay
         rts
dowash
         lda #0
         sta washdelay
         ldx washptr
         lda washtbl,x

         sta colr2+(12*40)-1
         sta colr2+(20*40)+39
         inx
         cpx #80
         beq loopwash
         inc washptr
         jmp washchars

loopwash
         ldx #0
         stx washptr
washchars
         ldx #$00
washback
         lda colr2+(20*40)+1,x
         sta colr2+(20*40),x
         inx
         cpx #40
         bne washback
         ldx #39
washfwd
         lda colr2+(12*40)-1,x
         sta colr2+(12*40),x
         dex
         bpl washfwd
         rts

colwash2
         lda washdelay2
         cmp #1
         beq dowash2
         inc washdelay2
         rts
dowash2
         lda #0
         sta washdelay2
         ldx washptr2
         lda washtbl2,x
         sta tcolstore
         lda washtbl3,x
         sta tcolstore2
         inx
         cpx #10;10 bytes read
         beq loopwash2
         inc washptr2
         jmp paintscrl
         rts

loopwash2
         ldx #0
         stx washptr2

;Paint colour data over the scroll

paintscrl
         lda tcolstore
         sta colr2+(21*40)+39
         ldx #$00
washscrl
         lda colr2+(21*40)+1,x
         sta colr2+(21*40),x
         lda tcolstore2
         sta colr2+(19*40),x
         inx
         cpx #40
         bne washscrl
         rts
;--------------------------------------

;Title screen pointers and tables
;--------------------------------------

; Smooth scroll control pointer

xpos     .byte 0

; Raster sync timer for title screen

rt2      .byte 0

; PAL/NTSC check pointer

system2  .byte 0

; Fire button press pointer

firebutton2 .byte 0

; Music delay pointer for NTSC systems

ntsctimer2 .byte 0

; Colour wash pointers

washdelay .byte 0
washdelay2 .byte 0
washptr  .byte 0
washptr2 .byte 0
tcolstore .byte 0
tcolstore2 .byte 0

; Colour washing tables

washtbl
         .byte $06,$04,$03,$07,$01
         .byte $01,$03,$04,$06,$06
         .byte $06,$06,$06,$06,$06
         .byte $06,$06,$06,$06,$06
         .byte $06,$06,$06,$06,$06
         .byte $06,$06,$06,$06,$06
         .byte $06,$06,$06,$06,$06
         .byte $06,$06,$06,$06,$06
         .byte $06,$06,$06,$06,$06
         .byte $06,$06,$06,$06,$06
         .byte $06,$06,$06,$06,$06
         .byte $06,$06,$06,$06,$06
         .byte $06,$06,$06,$06,$06
         .byte $06,$06,$06,$06,$06
         .byte $06,$06,$06,$06,$06
         .byte $06,$06,$06,$06,$06

; Colour washing table exclusive for
; text flashing

washtbl2
         .byte $06,$04,$0a,$07,$01
         .byte $07,$0a,$04,$06,$00

; Colour washing table exclusive for
; press fire to start text

washtbl3
         .byte $02,$04,$07,$01,$01
         .byte $01,$07,$04,$02,$00

